//
//  ContectInfoVC.m
//  HxgProject
//
//  Created by hxg on 2018/3/7.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "ReputationInfoVC.h"
#import "PersonInfoCell.h"
#import "RDVTabBarController.h"
#import "AuthenService.h"
#import "ResObject.h"

#import "NSString+Extention.h"
#import <IQKeyboardReturnKeyHandler.h>
#import "HxgTools.h"
#import "ReputationDetail.h"

@interface ReputationInfoVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
    
}
@property (nonatomic, strong) UITableView *contectInfoTableView;

// 配置数据源
@property (nonatomic, strong) NSMutableArray *personDataSource;


@property (nonatomic, strong) NSMutableArray *relationArr;

@end

@implementation ReputationInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}

- (void)setUpUI
{
    self.navigationItem.title = @"信誉认证";
    self.view.backgroundColor = defaultViewBgColor;
    [self.view addSubview:self.contectInfoTableView];
    [self.contectInfoTableView makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width,kScreen_Height - KIphoneSize_Height(44.0f) - KIphoneSize_Height(80.0f)));
    }];
    
    [self.contectInfoTableView registerClass:[PersonInfoCell class]
                      forCellReuseIdentifier:@"PersonCell"];
}

#pragma mark - tableViewDelegate
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    static NSString *defaultIdentifier = @"DefaultCell";
    static NSString *PersonIdentifier = @"PersonCell";
    
    if (2 != indexPath.row) {
       return [self getPersonCell:PersonIdentifier indexPath:indexPath tableView:tableView];
    } else {
       return [self getDefaultCell:defaultIdentifier indexPath:indexPath tableView:tableView];
    }
    
    return nil;
}

- (UITableViewCell * _Nonnull)getPersonCell:(NSString *)PersonIdentifier indexPath:(NSIndexPath * _Nonnull)indexPath tableView:(UITableView * _Nonnull)tableView {
    PersonInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:PersonIdentifier];
    
    if (!cell)
    {
        cell = [[PersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PersonIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSArray *arr = self.personDataSource[indexPath.section];
    cell.autherNameLbl.text = arr[indexPath.row];
    
    if (!isEmptyString(self.showReputationInfo.zfbAccount)
        || !isEmptyString(self.showReputationInfo.tbAccount)) {
        if (0 == indexPath.section) {
            if (indexPath.row == 0) {
                cell.autherContentField.text = self.showReputationInfo.zfbAccount;
            } else if(indexPath.row == 1){
                cell.autherContentField.text = self.showReputationInfo.zfbPwd;
            }
        } else {
            if (indexPath.row == 0) {
                cell.autherContentField.text = self.showReputationInfo.tbAccount;
            } else if(indexPath.row == 1){
                cell.autherContentField.text = self.showReputationInfo.tbPwd;
            }
        }
    }
    
    if (indexPath.row == 1) {
        cell.autherContentField.secureTextEntry = YES;
    }
    
    cell.autherContentField.delegate = self;
    cell.autherContentField.tag = indexPath.section * 10 + indexPath.row;
    [cell.autherContentField addTarget:self
                                action:@selector(getVal:)
                      forControlEvents:UIControlEventEditingChanged];
    
    return cell;
}

- (UITableViewCell * _Nonnull)getDefaultCell:(NSString *)defaultIdentifier indexPath:(NSIndexPath * _Nonnull)indexPath tableView:(UITableView * _Nonnull)tableView {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:defaultIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = defaultViewBgColor;
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (0 == indexPath.section) {
        submitBtn.tag = 0;
    } else if (1 == indexPath.section){
        submitBtn.tag = 1;
    }
    [submitBtn setBackgroundColor:kCommonBtnBgColor];
    submitBtn.layer.cornerRadius = 6.0f;
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [cell.contentView addSubview:submitBtn];
    
    [submitBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cell.contentView.mas_centerX);
        make.centerY.equalTo(cell.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(cell.contentView.frame.size.width / 3, 40.0f));
    }];
    [submitBtn addTarget:self
                  action:@selector(submitAction:)
        forControlEvents:UIControlEventTouchUpInside];
    
    // 是否对点击按钮灰化
    if (isXinYuAuthenSuc) {
        submitBtn.alpha = 0.4;
        submitBtn.enabled = NO;
    } else {
        submitBtn.alpha = 1;
        submitBtn.enabled = YES;
    }

    return cell;
}


- (void)getVal:(UITextField *)textField
{
    switch (textField.tag) {
            //第一个section
        case 0:
            self.showReputationInfo.zfbAccount = textField.text;
            break;
        case 1:
            self.showReputationInfo.zfbPwd = textField.text;
            break;
        case 10:
            self.showReputationInfo.tbAccount = textField.text;
            break;
        case 11:
            self.showReputationInfo.tbPwd = textField.text;
            break;
        default:
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (2 == indexPath.row) {
        return 80.0f;
    }
    
    return 60.0f;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - ###### section的头部跟尾部的高度 #####
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0 == section ? KIphoneSize_Height(10.0f) : 0.2;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.2f)];
        return view;
    }
    UIView *defaultView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, KIphoneSize_Height(0.1f))];
    defaultView.backgroundColor = defaultViewBgColor;
    return defaultView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0 == section ? KIphoneSize_Height(0.1f) : 0.2;
}

-  (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.2f)];
        return view;
    }
    UIView *defaultView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, KIphoneSize_Height(0.1f))];
    defaultView.backgroundColor = defaultViewBgColor;
    return defaultView;
}


#pragma mark - ###### custom event <##>######

- (void)submitAction:(UIButton *)submitBtn
{
    if (0 == submitBtn.tag) {
        if (isEmptyString(self.showReputationInfo.zfbAccount)
            || isEmptyString(self.showReputationInfo.zfbPwd)
            ) {
            [SVProgressHUD showErrorWithStatus:@"支付宝认证内容不能为空!"];
            [SVProgressHUD dismissWithDelay:2.0f];
            return;
        }
    } else if (1 == submitBtn.tag) {
        if (isEmptyString(self.showReputationInfo.tbAccount)
            || isEmptyString(self.showReputationInfo.tbPwd)
            ) {
            [SVProgressHUD showErrorWithStatus:@"淘宝认证内容不能为空!"];
            [SVProgressHUD dismissWithDelay:2.0f];
            return;
        }
    }
    
    __weak ReputationInfoVC *weakSelf = self;
    [SVProgressHUD showWithStatus:@"加载中..."];
    AuthenService *au = [[AuthenService alloc] init];
    [au saveReputationDetail:self.showReputationInfo withCallBack:^(BOOL isSuccess) {
        
        [SVProgressHUD dismiss];
        if (isSuccess)
        {
            [SVProgressHUD showSuccessWithStatus:@"操作成功!"];
            // 存储认证结果
            [self.navigationController popViewControllerAnimated:YES];
            
            submitBtn.enabled = NO;
            submitBtn.alpha = 0.4f;
            
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kXinYuAuthenRes];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"操作失败!"];
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kXinYuAuthenRes];
        }
        [SVProgressHUD dismissWithDelay:1.0f];
        [weakSelf.contectInfoTableView reloadData];
    }];
}

#pragma mark - ###### getter、setter ######
-(NSMutableArray *)personDataSource
{
    if (!_personDataSource)
    {
        _personDataSource = [[HxgTools sharedHxgTools] getProprotiesFormPlistFile:@"ReputationInfo"];
    }
    return _personDataSource;
}

-(UITableView *)contectInfoTableView
{
    if (!_contectInfoTableView)
    {
        _contectInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreen_Width, kScreen_Height - KIphoneSize_Height(44.0f) - KIphoneSize_Height(80.0f)) style:UITableViewStyleGrouped];
        _contectInfoTableView.delegate = self;
        _contectInfoTableView.dataSource = self;
        _contectInfoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _contectInfoTableView;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
    [super viewWillDisappear:animated];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kXinYuAuthenRes])
    {
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}


@end
